#include <iostream>

int const mo = 2015;
int const kk = 10000002;
int fact[kk];

long long quick(long long a, long long b)
{
	if (b == 0) return 1;
	long long t = quick(a, b/2);
	t = (t * t) % mo;
	if (b & 1) t *= a;
	return t % mo;
}

int main()
{
	fact[0] = 1;
	for (int i = 1; i < kk-1; i++) fact[i] = (fact[i-1] * i) % mo;
	int t, n, k;
	std::cin >> t;
	while (t--) {
		std::cin >> n >> k;
		long long ans = fact[k];
		ans *= (quick(k+1, n-k) - quick(k, n-k));
		ans %= mo;
		std::cout << (ans + mo) % mo << '\n';
	}
}

